//
//  QXMainVC.m
//  QXBlog
//
//  Created by QinXin on 15/7/5.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

#import "QXMainVC.h"
#import <Masonry/Masonry.h>

@interface QXMainVC ()

@end

@implementation QXMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 通过 本地 JSON 加载控制器 及 tabbar 样式
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ViewControllers.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *dicArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray *marr = [NSMutableArray array];
    for (NSDictionary *dic in dicArr) {
        UIViewController *vc = [self createViewControllerWithTitle:dic[@"title"]
                                                          iconName:dic[@"iconName"]
                                                            vcName:dic[@"vcName"]];
        [marr addObject:vc];
    }
    self.viewControllers = marr.copy;
    
    // 添加中心的大按钮
    UIButton *centerBtn = [[UIButton alloc] init];
    [centerBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [centerBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    [centerBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [centerBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [self.view addSubview:centerBtn];
    
    // 约束
    __weak typeof(self) ws = self;
    [centerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.view);
        make.bottom.equalTo(ws.view).with.offset(-3);
    }];
    
}

- (UIViewController *)createViewControllerWithTitle:(NSString *)title iconName:(NSString *)iconName vcName:(NSString *)vcName {
    UIViewController *vc = [[NSClassFromString(vcName) alloc] init];
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:iconName];
    return [[UINavigationController alloc] initWithRootViewController:vc];
}

@end
